{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<div style=\"text-align: center\"><font color='#dc2624' face='微软雅黑' size = \"6\">电子产品购物数据分析</font></div>**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\" # 代码块显示所有执行结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**<div style=\"text-align: left\"><font color='black' face='微软雅黑' size = \"6\">引言</font><a name='top'></a></div>**\n",
    "\n",
    "&emsp;&emsp;通过下载kaggle上的电子产品购物数据集，首先进行数据清洗，然后进行时间序列EDA，查看销售额随时间特征的变化情况并使用Echarts绘制相应的图形。再分别使用FP增长算法和Apriori算法计算频繁项集，然后计算关联规则以及对应几个指标。最后根据指标分析强关联商品之间的内在逻辑，并与销售部门制订相应的营销策略。<br/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#需要的包\" data-toc-modified-id=\"需要的包-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>需要的包</a></span></li><li><span><a href=\"#读取数据\" data-toc-modified-id=\"读取数据-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>读取数据</a></span></li><li><span><a href=\"#数据清洗\" data-toc-modified-id=\"数据清洗-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>数据清洗</a></span></li><li><span><a href=\"#时间序列EDA\" data-toc-modified-id=\"时间序列EDA-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>时间序列EDA</a></span><ul class=\"toc-item\"><li><span><a href=\"#时销售额历史数据\" data-toc-modified-id=\"时销售额历史数据-4.1\"><span class=\"toc-item-num\">4.1&nbsp;&nbsp;</span>时销售额历史数据</a></span></li><li><span><a href=\"#星期销售额历史数据\" data-toc-modified-id=\"星期销售额历史数据-4.2\"><span class=\"toc-item-num\">4.2&nbsp;&nbsp;</span>星期销售额历史数据</a></span></li><li><span><a href=\"#日销售额历史数据\" data-toc-modified-id=\"日销售额历史数据-4.3\"><span class=\"toc-item-num\">4.3&nbsp;&nbsp;</span>日销售额历史数据</a></span></li><li><span><a href=\"#周销售额历史数据\" data-toc-modified-id=\"周销售额历史数据-4.4\"><span class=\"toc-item-num\">4.4&nbsp;&nbsp;</span>周销售额历史数据</a></span></li><li><span><a href=\"#月销售额历史数据\" data-toc-modified-id=\"月销售额历史数据-4.5\"><span class=\"toc-item-num\">4.5&nbsp;&nbsp;</span>月销售额历史数据</a></span></li><li><span><a href=\"#季度销售额历史数据\" data-toc-modified-id=\"季度销售额历史数据-4.6\"><span class=\"toc-item-num\">4.6&nbsp;&nbsp;</span>季度销售额历史数据</a></span></li></ul></li><li><span><a href=\"#时间序列回归\" data-toc-modified-id=\"时间序列回归-5\"><span class=\"toc-item-num\">5&nbsp;&nbsp;</span>时间序列回归</a></span></li><li><span><a href=\"#关联规则\" data-toc-modified-id=\"关联规则-6\"><span class=\"toc-item-num\">6&nbsp;&nbsp;</span>关联规则</a></span><ul class=\"toc-item\"><li><span><a href=\"#计算事务集\" data-toc-modified-id=\"计算事务集-6.1\"><span class=\"toc-item-num\">6.1&nbsp;&nbsp;</span>计算事务集</a></span></li><li><span><a href=\"#哑变量矩阵\" data-toc-modified-id=\"哑变量矩阵-6.2\"><span class=\"toc-item-num\">6.2&nbsp;&nbsp;</span>哑变量矩阵</a></span></li><li><span><a href=\"#FP-增长算法计算频繁项集\" data-toc-modified-id=\"FP-增长算法计算频繁项集-6.3\"><span class=\"toc-item-num\">6.3&nbsp;&nbsp;</span>FP-增长算法计算频繁项集</a></span></li><li><span><a href=\"#Apriori算法版本\" data-toc-modified-id=\"Apriori算法版本-6.4\"><span class=\"toc-item-num\">6.4&nbsp;&nbsp;</span>Apriori算法版本</a></span></li><li><span><a href=\"#计算规则和支持度\" data-toc-modified-id=\"计算规则和支持度-6.5\"><span class=\"toc-item-num\">6.5&nbsp;&nbsp;</span>计算规则和支持度</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 需要的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据处理\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 绘图\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.charts import Line\n",
    "from pyecharts.charts import Pie\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "# 关联规则\n",
    "from mlxtend.preprocessing import TransactionEncoder\n",
    "from mlxtend.frequent_patterns import fpgrowth\n",
    "from mlxtend.frequent_patterns import apriori\n",
    "from mlxtend.frequent_patterns import association_rules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取数据\n",
    "数据来源: https://www.kaggle.com/mkechinov/ecommerce-purchase-history-from-electronics-store"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 3min 47s\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_time</th>\n",
       "      <th>order_id</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category_id</th>\n",
       "      <th>category_code</th>\n",
       "      <th>brand</th>\n",
       "      <th>price</th>\n",
       "      <th>user_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-04-24 11:50:39+00:00</td>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>1515966223509089906</td>\n",
       "      <td>2268105426648170900</td>\n",
       "      <td>electronics.tablet</td>\n",
       "      <td>samsung</td>\n",
       "      <td>162.01</td>\n",
       "      <td>1515915625441993984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-04-24 11:50:39+00:00</td>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>1515966223509089906</td>\n",
       "      <td>2268105426648170900</td>\n",
       "      <td>electronics.tablet</td>\n",
       "      <td>samsung</td>\n",
       "      <td>162.01</td>\n",
       "      <td>1515915625441993984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-04-24 14:37:43+00:00</td>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>\n",
       "      <td>2268105430162997728</td>\n",
       "      <td>electronics.audio.headphone</td>\n",
       "      <td>huawei</td>\n",
       "      <td>77.52</td>\n",
       "      <td>1515915625447879434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-04-24 14:37:43+00:00</td>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>\n",
       "      <td>2268105430162997728</td>\n",
       "      <td>electronics.audio.headphone</td>\n",
       "      <td>huawei</td>\n",
       "      <td>77.52</td>\n",
       "      <td>1515915625447879434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-04-24 19:16:21+00:00</td>\n",
       "      <td>2294584263154074236</td>\n",
       "      <td>2273948316817424439</td>\n",
       "      <td>2268105471367840086</td>\n",
       "      <td>NaN</td>\n",
       "      <td>karcher</td>\n",
       "      <td>217.57</td>\n",
       "      <td>1515915625443148002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 event_time             order_id           product_id  \\\n",
       "0 2020-04-24 11:50:39+00:00  2294359932054536986  1515966223509089906   \n",
       "1 2020-04-24 11:50:39+00:00  2294359932054536986  1515966223509089906   \n",
       "2 2020-04-24 14:37:43+00:00  2294444024058086220  2273948319057183658   \n",
       "3 2020-04-24 14:37:43+00:00  2294444024058086220  2273948319057183658   \n",
       "4 2020-04-24 19:16:21+00:00  2294584263154074236  2273948316817424439   \n",
       "\n",
       "           category_id                category_code    brand   price  \\\n",
       "0  2268105426648170900           electronics.tablet  samsung  162.01   \n",
       "1  2268105426648170900           electronics.tablet  samsung  162.01   \n",
       "2  2268105430162997728  electronics.audio.headphone   huawei   77.52   \n",
       "3  2268105430162997728  electronics.audio.headphone   huawei   77.52   \n",
       "4  2268105471367840086                          NaN  karcher  217.57   \n",
       "\n",
       "               user_id  \n",
       "0  1515915625441993984  \n",
       "1  1515915625441993984  \n",
       "2  1515915625447879434  \n",
       "3  1515915625447879434  \n",
       "4  1515915625443148002  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "df1 = pd.read_csv('F:/py_input_output/data/kz.csv', \n",
    "                  parse_dates=['event_time'], \n",
    "                  dtype={'order_id': 'string', 'product_id': 'string', \n",
    "                         'category_id': 'string', 'user_id': 'string'}) # 必须这样解析，因为数字太大，且存在缺失值\n",
    "df1.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据清洗"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2633521, 8)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "event_time       datetime64[ns, UTC]\n",
       "order_id                      string\n",
       "product_id                    string\n",
       "category_id                   string\n",
       "category_code                 object\n",
       "brand                         object\n",
       "price                        float64\n",
       "user_id                       string\n",
       "dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.shape # 26万多行\n",
    "df1.dtypes # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "event_time             0\n",
       "order_id               0\n",
       "product_id             0\n",
       "category_id       431954\n",
       "category_code     612202\n",
       "brand             506005\n",
       "price             431954\n",
       "user_id          2069352\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "event_time        0.000000\n",
       "order_id          0.000000\n",
       "product_id        0.000000\n",
       "category_id      16.402148\n",
       "category_code    23.246521\n",
       "brand            19.214010\n",
       "price            16.402148\n",
       "user_id          78.577387\n",
       "dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.isna().sum(axis=0) # 缺失值数量\n",
    "df1.isna().agg(lambda x: x.sum(axis=0)/x.shape[0] * 100) # 缺失值比例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;从缺失值统计结果来看, 字段'user_id'缺失比例过高，超过78%，因此这个字段意义不大。价格也有很多缺失值。\n",
    "因此这个数据只能用来进行购物篮分析，分析用户购买商品之间的关联。因此只需要保留'event_time', 'order_id', 'product_id', 'category_id'这4个字段。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1314, 2)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将商品分类数据单独提出来，备用\n",
    "df_category = df1[['category_id', 'category_code']].drop_duplicates()\n",
    "df_category.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2201567, 5)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_time</th>\n",
       "      <th>order_id</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category_id</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-04-24 11:50:39+00:00</td>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>1515966223509089906</td>\n",
       "      <td>2268105426648170900</td>\n",
       "      <td>162.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-04-24 11:50:39+00:00</td>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>1515966223509089906</td>\n",
       "      <td>2268105426648170900</td>\n",
       "      <td>162.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-04-24 14:37:43+00:00</td>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>\n",
       "      <td>2268105430162997728</td>\n",
       "      <td>77.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-04-24 14:37:43+00:00</td>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>\n",
       "      <td>2268105430162997728</td>\n",
       "      <td>77.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-04-24 19:16:21+00:00</td>\n",
       "      <td>2294584263154074236</td>\n",
       "      <td>2273948316817424439</td>\n",
       "      <td>2268105471367840086</td>\n",
       "      <td>217.57</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 event_time             order_id           product_id  \\\n",
       "0 2020-04-24 11:50:39+00:00  2294359932054536986  1515966223509089906   \n",
       "1 2020-04-24 11:50:39+00:00  2294359932054536986  1515966223509089906   \n",
       "2 2020-04-24 14:37:43+00:00  2294444024058086220  2273948319057183658   \n",
       "3 2020-04-24 14:37:43+00:00  2294444024058086220  2273948319057183658   \n",
       "4 2020-04-24 19:16:21+00:00  2294584263154074236  2273948316817424439   \n",
       "\n",
       "           category_id   price  \n",
       "0  2268105426648170900  162.01  \n",
       "1  2268105426648170900  162.01  \n",
       "2  2268105430162997728   77.52  \n",
       "3  2268105430162997728   77.52  \n",
       "4  2268105471367840086  217.57  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = (df1.reindex(['event_time', 'order_id', 'product_id', 'category_id', 'price'], axis=1)\n",
    "       .dropna(how='any')\n",
    "       .reset_index(drop=True)\n",
    "      )\n",
    "df2.shape\n",
    "df2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "product_id     23809\n",
       "category_id      927\n",
       "dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2[['product_id', 'category_id']].nunique() # 商品种类2万多，9百多个分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2020-11-21 10:10:30+0000', tz='UTC')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Timestamp('1970-01-01 00:33:40+0000', tz='UTC')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2['event_time'].max()\n",
    "df2['event_time'].min()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "时间范围比较长，可以增加日期特征、年份特征、月份特征、星期特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_time</th>\n",
       "      <th>event_date</th>\n",
       "      <th>event_year</th>\n",
       "      <th>order_id</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category_id</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-04-24 11:50:39+00:00</td>\n",
       "      <td>2020-04-24 00:00:00+00:00</td>\n",
       "      <td>2020</td>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>1515966223509089906</td>\n",
       "      <td>2268105426648170900</td>\n",
       "      <td>162.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-04-24 11:50:39+00:00</td>\n",
       "      <td>2020-04-24 00:00:00+00:00</td>\n",
       "      <td>2020</td>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>1515966223509089906</td>\n",
       "      <td>2268105426648170900</td>\n",
       "      <td>162.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-04-24 14:37:43+00:00</td>\n",
       "      <td>2020-04-24 00:00:00+00:00</td>\n",
       "      <td>2020</td>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>\n",
       "      <td>2268105430162997728</td>\n",
       "      <td>77.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-04-24 14:37:43+00:00</td>\n",
       "      <td>2020-04-24 00:00:00+00:00</td>\n",
       "      <td>2020</td>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>\n",
       "      <td>2268105430162997728</td>\n",
       "      <td>77.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-04-24 19:16:21+00:00</td>\n",
       "      <td>2020-04-24 00:00:00+00:00</td>\n",
       "      <td>2020</td>\n",
       "      <td>2294584263154074236</td>\n",
       "      <td>2273948316817424439</td>\n",
       "      <td>2268105471367840086</td>\n",
       "      <td>217.57</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 event_time                event_date  event_year  \\\n",
       "0 2020-04-24 11:50:39+00:00 2020-04-24 00:00:00+00:00        2020   \n",
       "1 2020-04-24 11:50:39+00:00 2020-04-24 00:00:00+00:00        2020   \n",
       "2 2020-04-24 14:37:43+00:00 2020-04-24 00:00:00+00:00        2020   \n",
       "3 2020-04-24 14:37:43+00:00 2020-04-24 00:00:00+00:00        2020   \n",
       "4 2020-04-24 19:16:21+00:00 2020-04-24 00:00:00+00:00        2020   \n",
       "\n",
       "              order_id           product_id          category_id   price  \n",
       "0  2294359932054536986  1515966223509089906  2268105426648170900  162.01  \n",
       "1  2294359932054536986  1515966223509089906  2268105426648170900  162.01  \n",
       "2  2294444024058086220  2273948319057183658  2268105430162997728   77.52  \n",
       "3  2294444024058086220  2273948319057183658  2268105430162997728   77.52  \n",
       "4  2294584263154074236  2273948316817424439  2268105471367840086  217.57  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = (df2.eval('event_year = event_time.dt.year', engine='python') # 年特征\n",
    "       .eval('event_date = event_time.dt.normalize()', engine='python') # 日期特征\n",
    "       .reindex(['event_time', 'event_date', 'event_year', 'order_id', 'product_id', 'category_id', 'price'], axis=1)\n",
    "      )\n",
    "df3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_year</th>\n",
       "      <th>freq</th>\n",
       "      <th>perct</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020</td>\n",
       "      <td>2185895</td>\n",
       "      <td>99.293601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1970</td>\n",
       "      <td>15551</td>\n",
       "      <td>0.706399</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   event_year     freq      perct\n",
       "0        2020  2185895  99.293601\n",
       "1        1970    15551   0.706399"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(df3.query('price != 0')['event_year']\n",
    " .value_counts()\n",
    " .reset_index()\n",
    " .rename({'index': 'event_year', 'event_year': 'freq'}, axis=1)\n",
    " .pipe(lambda x: x.assign(perct = x['freq']/x['freq'].sum()*100)\n",
    "      )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从时间分布上看，99%以上的交易都发生在2020年，考虑到电子产品升级换代速度快，1970的在售的电子产品，几乎不可能2020年还在销售。\n",
    "因此只对2020年以后的数据进行后续分析。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2186014, 7)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4 = df3.query(\"event_year >= 2020\")\n",
    "df4.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 时间序列EDA\n",
    "没有单个订单的商品数量这个字段，所以没法统计销售数量，只能统计销售额"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 时销售额历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>event_hour</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>945805.47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1285250.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2080710.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4279326.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>11822235.41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  price\n",
       "event_hour             \n",
       "0             945805.47\n",
       "1            1285250.33\n",
       "2            2080710.26\n",
       "3            4279326.41\n",
       "4           11822235.41"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sum_price_hourly = (df4.reindex(['event_time', 'price'], axis=1)\n",
    "                       .eval(\"event_hour = event_time.dt.hour\", engine='python')\n",
    "                       .drop('event_time', axis=1)\n",
    "                       .set_index('event_hour')\n",
    "                       .groupby(level='event_hour')\n",
    "                       .sum()\n",
    "                      )\n",
    "df_sum_price_hourly.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min', 'chalk':'https://assets.pyecharts.org/assets/themes/chalk'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"e4ece9a3b2194d62b8a301b8e1448d6b\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'chalk'], function(echarts) {\n",
       "                var chart_e4ece9a3b2194d62b8a301b8e1448d6b = echarts.init(\n",
       "                    document.getElementById('e4ece9a3b2194d62b8a301b8e1448d6b'), 'chalk', {renderer: 'canvas'});\n",
       "                var option_e4ece9a3b2194d62b8a301b8e1448d6b = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 10,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    0,\n",
       "                    945805.47\n",
       "                ],\n",
       "                [\n",
       "                    1,\n",
       "                    1285250.33\n",
       "                ],\n",
       "                [\n",
       "                    2,\n",
       "                    2080710.26\n",
       "                ],\n",
       "                [\n",
       "                    3,\n",
       "                    4279326.41\n",
       "                ],\n",
       "                [\n",
       "                    4,\n",
       "                    11822235.41\n",
       "                ],\n",
       "                [\n",
       "                    5,\n",
       "                    23449998.08\n",
       "                ],\n",
       "                [\n",
       "                    6,\n",
       "                    29653004.99\n",
       "                ],\n",
       "                [\n",
       "                    7,\n",
       "                    32394703.74\n",
       "                ],\n",
       "                [\n",
       "                    8,\n",
       "                    33252474.41\n",
       "                ],\n",
       "                [\n",
       "                    9,\n",
       "                    35592886.29\n",
       "                ],\n",
       "                [\n",
       "                    10,\n",
       "                    37077541.11\n",
       "                ],\n",
       "                [\n",
       "                    11,\n",
       "                    34557437.53\n",
       "                ],\n",
       "                [\n",
       "                    12,\n",
       "                    28756897.16\n",
       "                ],\n",
       "                [\n",
       "                    13,\n",
       "                    23439007.94\n",
       "                ],\n",
       "                [\n",
       "                    14,\n",
       "                    14826077.61\n",
       "                ],\n",
       "                [\n",
       "                    15,\n",
       "                    8771181.38\n",
       "                ],\n",
       "                [\n",
       "                    16,\n",
       "                    4998154.61\n",
       "                ],\n",
       "                [\n",
       "                    17,\n",
       "                    3165080.42\n",
       "                ],\n",
       "                [\n",
       "                    18,\n",
       "                    2311433.67\n",
       "                ],\n",
       "                [\n",
       "                    19,\n",
       "                    1313325.21\n",
       "                ],\n",
       "                [\n",
       "                    20,\n",
       "                    886684.04\n",
       "                ],\n",
       "                [\n",
       "                    21,\n",
       "                    686864.61\n",
       "                ],\n",
       "                [\n",
       "                    22,\n",
       "                    712305.84\n",
       "                ],\n",
       "                [\n",
       "                    23,\n",
       "                    813168.59\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                0,\n",
       "                1,\n",
       "                2,\n",
       "                3,\n",
       "                4,\n",
       "                5,\n",
       "                6,\n",
       "                7,\n",
       "                8,\n",
       "                9,\n",
       "                10,\n",
       "                11,\n",
       "                12,\n",
       "                13,\n",
       "                14,\n",
       "                15,\n",
       "                16,\n",
       "                17,\n",
       "                18,\n",
       "                19,\n",
       "                20,\n",
       "                21,\n",
       "                22,\n",
       "                23\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u65f6\\u9500\\u552e\\u989d\\u66f2\\u7ebf\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_e4ece9a3b2194d62b8a301b8e1448d6b.setOption(option_e4ece9a3b2194d62b8a301b8e1448d6b);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x231dc65cf40>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    Line(init_opts=opts.InitOpts(theme=ThemeType.CHALK))\n",
    "    .add_xaxis(xaxis_data = df_sum_price_hourly.index.tolist())\n",
    "    .add_yaxis(\"\", df_sum_price_hourly['price'].tolist(), \n",
    "               is_symbol_show=True, # 显示点\n",
    "               symbol_size=10 # 设置点尺寸\n",
    "              ) \n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"时销售额曲线\"))\n",
    "    .render_notebook()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;从图中可以看出，大量的交易都发生在上午8点到12点，而晚上的销售额很少，这应该与当地习惯有关，因此如果能在白天和晚上，实行不同的促销策略和广告投放，对提高销售业绩会有很大帮助。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 星期销售额历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>weekday</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>星期一</td>\n",
       "      <td>47270030.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>星期二</td>\n",
       "      <td>48061546.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>星期三</td>\n",
       "      <td>45248518.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>星期四</td>\n",
       "      <td>44083635.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>星期五</td>\n",
       "      <td>51639473.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>星期六</td>\n",
       "      <td>51325123.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>星期日</td>\n",
       "      <td>49443226.51</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   weekday        price\n",
       "id                     \n",
       "0      星期一  47270030.73\n",
       "1      星期二  48061546.99\n",
       "2      星期三  45248518.41\n",
       "3      星期四  44083635.69\n",
       "4      星期五  51639473.40\n",
       "5      星期六  51325123.38\n",
       "6      星期日  49443226.51"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weekday_df = pd.DataFrame(\n",
    "    {\n",
    "        'id': [0, 1, 2, 3, 4, 5, 6],\n",
    "        'weekday': ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']\n",
    "    }\n",
    ").set_index('id')\n",
    "\n",
    "df_sum_price_weekdayly = (df4.reindex(['event_date', 'price'], axis=1)\n",
    "                          .eval('id = event_date.dt.weekday', engine='python')\n",
    "                          .drop('event_date', axis=1)\n",
    "                          .set_index('id')\n",
    "                          .groupby(level='id')\n",
    "                          .sum()\n",
    "                          .join(weekday_df, how='left')\n",
    "                          .sort_index(axis=0)\n",
    "                          .reindex(['weekday', 'price'], axis=1)\n",
    "                         )\n",
    "df_sum_price_weekdayly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min', 'chalk':'https://assets.pyecharts.org/assets/themes/chalk'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"7d19670d6f18484285f16f80308aad49\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'chalk'], function(echarts) {\n",
       "                var chart_7d19670d6f18484285f16f80308aad49 = echarts.init(\n",
       "                    document.getElementById('7d19670d6f18484285f16f80308aad49'), 'chalk', {renderer: 'canvas'});\n",
       "                var option_7d19670d6f18484285f16f80308aad49 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 10,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    \"\\u661f\\u671f\\u4e00\",\n",
       "                    47270030.73\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u661f\\u671f\\u4e8c\",\n",
       "                    48061546.99\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u661f\\u671f\\u4e09\",\n",
       "                    45248518.41\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u661f\\u671f\\u56db\",\n",
       "                    44083635.69\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u661f\\u671f\\u4e94\",\n",
       "                    51639473.4\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u661f\\u671f\\u516d\",\n",
       "                    51325123.38\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u661f\\u671f\\u65e5\",\n",
       "                    49443226.51\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"\\u661f\\u671f\\u4e00\",\n",
       "                \"\\u661f\\u671f\\u4e8c\",\n",
       "                \"\\u661f\\u671f\\u4e09\",\n",
       "                \"\\u661f\\u671f\\u56db\",\n",
       "                \"\\u661f\\u671f\\u4e94\",\n",
       "                \"\\u661f\\u671f\\u516d\",\n",
       "                \"\\u661f\\u671f\\u65e5\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u661f\\u671f\\u9500\\u552e\\u989d\\u66f2\\u7ebf\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_7d19670d6f18484285f16f80308aad49.setOption(option_7d19670d6f18484285f16f80308aad49);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x231c5742130>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    Line(init_opts=opts.InitOpts(theme=ThemeType.CHALK))\n",
    "    .add_xaxis(xaxis_data = df_sum_price_weekdayly['weekday'].tolist())\n",
    "    .add_yaxis(\"\", df_sum_price_weekdayly['price'].tolist(), \n",
    "               is_symbol_show=True, # 显示点\n",
    "               symbol_size=10 # 设置点尺寸\n",
    "              ) \n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"星期销售额曲线\"))\n",
    "    .render_notebook()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 日销售额历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>event_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-05 00:00:00+00:00</th>\n",
       "      <td>1162961.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-06 00:00:00+00:00</th>\n",
       "      <td>1026495.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-07 00:00:00+00:00</th>\n",
       "      <td>1385809.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-08 00:00:00+00:00</th>\n",
       "      <td>899641.47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-09 00:00:00+00:00</th>\n",
       "      <td>796164.96</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                price\n",
       "event_date                           \n",
       "2020-01-05 00:00:00+00:00  1162961.27\n",
       "2020-01-06 00:00:00+00:00  1026495.28\n",
       "2020-01-07 00:00:00+00:00  1385809.76\n",
       "2020-01-08 00:00:00+00:00   899641.47\n",
       "2020-01-09 00:00:00+00:00   796164.96"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sum_price_daily = (df4.reindex(['event_date', 'price'], axis=1)\n",
    "                      .set_index('event_date')\n",
    "                      .groupby(pd.Grouper(freq='1D', level='event_date'))\n",
    "                      .sum()\n",
    "                     )\n",
    "df_sum_price_daily.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min', 'chalk':'https://assets.pyecharts.org/assets/themes/chalk'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"a4db56a340414b2280b125038c984795\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'chalk'], function(echarts) {\n",
       "                var chart_a4db56a340414b2280b125038c984795 = echarts.init(\n",
       "                    document.getElementById('a4db56a340414b2280b125038c984795'), 'chalk', {renderer: 'canvas'});\n",
       "                var option_a4db56a340414b2280b125038c984795 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 4,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    \"2020-01-05\",\n",
       "                    1162961.27\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-06\",\n",
       "                    1026495.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-07\",\n",
       "                    1385809.76\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-08\",\n",
       "                    899641.47\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-09\",\n",
       "                    796164.96\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-10\",\n",
       "                    1232681.21\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-11\",\n",
       "                    1519425.32\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-12\",\n",
       "                    1416036.89\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-13\",\n",
       "                    801136.82\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-14\",\n",
       "                    809235.29\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-15\",\n",
       "                    769931.88\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-16\",\n",
       "                    736832.58\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-17\",\n",
       "                    824428.84\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-18\",\n",
       "                    1258192.33\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-19\",\n",
       "                    1234279.04\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-20\",\n",
       "                    707696.56\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-21\",\n",
       "                    762008.69\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-22\",\n",
       "                    686308.22\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-23\",\n",
       "                    619453.63\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-24\",\n",
       "                    1258593.78\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-25\",\n",
       "                    1599197.32\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-26\",\n",
       "                    1661056.35\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-27\",\n",
       "                    614551.32\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-28\",\n",
       "                    604953.72\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-29\",\n",
       "                    547614.96\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-30\",\n",
       "                    699629.44\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-31\",\n",
       "                    860081.6\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-01\",\n",
       "                    1565608.2\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-02\",\n",
       "                    1556315.1\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-03\",\n",
       "                    708427.43\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-04\",\n",
       "                    803228.53\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-05\",\n",
       "                    739246.12\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-06\",\n",
       "                    860668.06\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-07\",\n",
       "                    1367859.51\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-08\",\n",
       "                    1860866.31\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-09\",\n",
       "                    1754750.05\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-10\",\n",
       "                    788829.48\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-11\",\n",
       "                    762400.98\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-12\",\n",
       "                    720446.29\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-13\",\n",
       "                    905624.63\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-14\",\n",
       "                    2207843.1\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-15\",\n",
       "                    1722792.49\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-16\",\n",
       "                    1807731.77\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-17\",\n",
       "                    628579.56\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-18\",\n",
       "                    667248.26\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-19\",\n",
       "                    612197.08\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-20\",\n",
       "                    717679.34\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-21\",\n",
       "                    894988.65\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-22\",\n",
       "                    1484650.11\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-23\",\n",
       "                    1496782.02\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-24\",\n",
       "                    589345.89\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-25\",\n",
       "                    617084.07\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-26\",\n",
       "                    541882.14\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-27\",\n",
       "                    537950.59\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-28\",\n",
       "                    1295608.51\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-29\",\n",
       "                    1620299.33\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-01\",\n",
       "                    1563181.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-02\",\n",
       "                    664186.58\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-03\",\n",
       "                    688935.37\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-04\",\n",
       "                    681069.84\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-05\",\n",
       "                    1005143.73\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-06\",\n",
       "                    1667593.04\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-07\",\n",
       "                    2522502.44\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-08\",\n",
       "                    2383107.23\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-09\",\n",
       "                    2371906.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-10\",\n",
       "                    845413.79\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-11\",\n",
       "                    849256.5\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-12\",\n",
       "                    931072.16\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-13\",\n",
       "                    1162783.17\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-14\",\n",
       "                    1779360.91\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-15\",\n",
       "                    2094393.09\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-16\",\n",
       "                    944476.23\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-17\",\n",
       "                    1004742.13\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-18\",\n",
       "                    1050914.92\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-19\",\n",
       "                    1149096.38\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-20\",\n",
       "                    1249448.57\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-21\",\n",
       "                    1512306.98\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-22\",\n",
       "                    1306994.2\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-23\",\n",
       "                    939114.58\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-24\",\n",
       "                    1104776.3800000001\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-25\",\n",
       "                    1095336.83\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-26\",\n",
       "                    767453.17\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-27\",\n",
       "                    935746.3\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-28\",\n",
       "                    1150138.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-29\",\n",
       "                    1097891.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-30\",\n",
       "                    490096.43\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-31\",\n",
       "                    542529.25\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-01\",\n",
       "                    337621.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-02\",\n",
       "                    354534.31\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-03\",\n",
       "                    257524.7\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-04\",\n",
       "                    380099.06\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-05\",\n",
       "                    266649.44\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-06\",\n",
       "                    242556.12\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-07\",\n",
       "                    240336.47\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-08\",\n",
       "                    179617.24\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-09\",\n",
       "                    191093.42\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-10\",\n",
       "                    268624.74\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-11\",\n",
       "                    322660.98\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-12\",\n",
       "                    309054.64\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-13\",\n",
       "                    265981.99\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-14\",\n",
       "                    297458.71\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-15\",\n",
       "                    281460.14\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-16\",\n",
       "                    255435.87\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-17\",\n",
       "                    150702.8\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-18\",\n",
       "                    166930.67\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-19\",\n",
       "                    185555.21\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-20\",\n",
       "                    180576.88\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-21\",\n",
       "                    206818.23\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-22\",\n",
       "                    147676.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-23\",\n",
       "                    225831.81\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-24\",\n",
       "                    299620.37\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-25\",\n",
       "                    306015.49\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-26\",\n",
       "                    345591.93\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-27\",\n",
       "                    211108.65\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-28\",\n",
       "                    255156.45\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-29\",\n",
       "                    249088.83\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-30\",\n",
       "                    308241.9\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-01\",\n",
       "                    297970.72\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-02\",\n",
       "                    319881.36\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-03\",\n",
       "                    304538.0\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-04\",\n",
       "                    335249.29\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-05\",\n",
       "                    458616.91\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-06\",\n",
       "                    373104.7\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-07\",\n",
       "                    562020.72\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-08\",\n",
       "                    509329.99\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-09\",\n",
       "                    508501.05\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-10\",\n",
       "                    502351.13\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-11\",\n",
       "                    607233.39\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-12\",\n",
       "                    626736.09\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-13\",\n",
       "                    649142.71\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-14\",\n",
       "                    717031.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-15\",\n",
       "                    740653.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-16\",\n",
       "                    1109251.95\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-17\",\n",
       "                    1105887.78\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-18\",\n",
       "                    683959.83\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-19\",\n",
       "                    742338.21\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-20\",\n",
       "                    717355.61\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-21\",\n",
       "                    1162245.86\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-22\",\n",
       "                    1321930.86\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-23\",\n",
       "                    1870168.4\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-24\",\n",
       "                    1832573.2\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-25\",\n",
       "                    1052718.02\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-26\",\n",
       "                    1232296.86\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-27\",\n",
       "                    1188934.65\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-28\",\n",
       "                    1125017.8\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-29\",\n",
       "                    1408271.5\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-30\",\n",
       "                    2127243.05\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-31\",\n",
       "                    2154421.85\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-01\",\n",
       "                    1116561.65\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-02\",\n",
       "                    1269668.2\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-03\",\n",
       "                    1444080.87\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-04\",\n",
       "                    1459375.37\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-05\",\n",
       "                    1623043.91\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-06\",\n",
       "                    2313528.13\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-07\",\n",
       "                    2635791.02\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-08\",\n",
       "                    1339822.9\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-09\",\n",
       "                    1467554.79\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-10\",\n",
       "                    1448941.52\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-11\",\n",
       "                    1379068.51\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-12\",\n",
       "                    1706851.61\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-13\",\n",
       "                    2225668.79\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-14\",\n",
       "                    2498940.86\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-15\",\n",
       "                    1235564.71\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-16\",\n",
       "                    1281937.22\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-17\",\n",
       "                    1356170.75\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-18\",\n",
       "                    1518168.86\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-19\",\n",
       "                    1807326.47\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-20\",\n",
       "                    1051068.09\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-21\",\n",
       "                    922883.4\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-22\",\n",
       "                    1411667.73\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-23\",\n",
       "                    1419993.0\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-24\",\n",
       "                    1346571.3\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-25\",\n",
       "                    1430157.84\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-26\",\n",
       "                    1686038.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-27\",\n",
       "                    350881.76\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-28\",\n",
       "                    223142.0\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-29\",\n",
       "                    1825635.82\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-30\",\n",
       "                    2277674.23\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-01\",\n",
       "                    1072118.9\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-02\",\n",
       "                    1440670.03\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-03\",\n",
       "                    1897629.73\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-04\",\n",
       "                    2051862.48\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-05\",\n",
       "                    226904.82\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-06\",\n",
       "                    318654.84\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-07\",\n",
       "                    388525.46\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-08\",\n",
       "                    441453.71\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-09\",\n",
       "                    463881.74\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-10\",\n",
       "                    470698.68\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-11\",\n",
       "                    437319.27\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-12\",\n",
       "                    401473.02\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-13\",\n",
       "                    444990.49\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-14\",\n",
       "                    693128.96\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-15\",\n",
       "                    551539.96\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-16\",\n",
       "                    673065.06\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-17\",\n",
       "                    769974.48\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-18\",\n",
       "                    684871.29\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-19\",\n",
       "                    590628.29\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-20\",\n",
       "                    443688.11\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-21\",\n",
       "                    715330.93\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-22\",\n",
       "                    679834.55\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-23\",\n",
       "                    1084533.43\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-24\",\n",
       "                    1233676.01\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-25\",\n",
       "                    991716.42\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-26\",\n",
       "                    1106856.93\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-27\",\n",
       "                    1406176.24\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-28\",\n",
       "                    1297910.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-29\",\n",
       "                    1340356.53\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-30\",\n",
       "                    902206.3\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-31\",\n",
       "                    986085.52\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-01\",\n",
       "                    1276030.27\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-02\",\n",
       "                    1310605.66\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-03\",\n",
       "                    1679241.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-04\",\n",
       "                    1511012.64\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-05\",\n",
       "                    1744029.84\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-06\",\n",
       "                    1482845.1\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-07\",\n",
       "                    1197334.8\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-08\",\n",
       "                    1436442.33\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-09\",\n",
       "                    1415335.79\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-10\",\n",
       "                    1623914.33\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-11\",\n",
       "                    1501078.7\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-12\",\n",
       "                    1159478.7\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-13\",\n",
       "                    1227378.12\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-14\",\n",
       "                    1086901.5\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-15\",\n",
       "                    954279.73\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-16\",\n",
       "                    813053.93\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-17\",\n",
       "                    3510234.4\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-18\",\n",
       "                    3076835.36\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-19\",\n",
       "                    2648158.39\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-20\",\n",
       "                    2143810.61\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-21\",\n",
       "                    2579700.58\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-22\",\n",
       "                    519265.93\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-23\",\n",
       "                    714694.19\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-24\",\n",
       "                    2945542.54\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-25\",\n",
       "                    2620444.4\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-26\",\n",
       "                    2543320.57\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-27\",\n",
       "                    2427907.73\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-28\",\n",
       "                    2726654.82\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-29\",\n",
       "                    723524.78\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-30\",\n",
       "                    811853.42\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-31\",\n",
       "                    1415017.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-01\",\n",
       "                    2566705.98\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-02\",\n",
       "                    2105479.23\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-03\",\n",
       "                    1875282.8\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-04\",\n",
       "                    2132596.0\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-05\",\n",
       "                    653389.44\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-06\",\n",
       "                    531014.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-07\",\n",
       "                    2224821.22\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-08\",\n",
       "                    1879140.67\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-09\",\n",
       "                    1920656.08\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-10\",\n",
       "                    1931443.82\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-11\",\n",
       "                    2312809.13\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-12\",\n",
       "                    927610.39\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-13\",\n",
       "                    808694.86\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-14\",\n",
       "                    2071156.63\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-15\",\n",
       "                    2091799.67\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-16\",\n",
       "                    2009072.21\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-17\",\n",
       "                    1810714.83\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-18\",\n",
       "                    1949911.16\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-19\",\n",
       "                    950274.35\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-20\",\n",
       "                    825769.4400000001\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-21\",\n",
       "                    2154206.32\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-22\",\n",
       "                    1742609.49\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-23\",\n",
       "                    1564498.98\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-24\",\n",
       "                    1507813.25\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-25\",\n",
       "                    1508694.23\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-26\",\n",
       "                    787118.89\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-27\",\n",
       "                    622462.71\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-28\",\n",
       "                    1842589.3\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-29\",\n",
       "                    1847581.78\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-30\",\n",
       "                    2162070.98\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-01\",\n",
       "                    495162.45\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-02\",\n",
       "                    564637.93\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-03\",\n",
       "                    961567.48\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-04\",\n",
       "                    780727.12\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-05\",\n",
       "                    743663.48\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-06\",\n",
       "                    502868.59\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-07\",\n",
       "                    577054.18\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-08\",\n",
       "                    529737.81\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-09\",\n",
       "                    629812.09\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-10\",\n",
       "                    1095891.22\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-11\",\n",
       "                    868201.37\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-12\",\n",
       "                    415408.1\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-13\",\n",
       "                    596083.28\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-14\",\n",
       "                    537325.49\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-15\",\n",
       "                    445586.25\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-16\",\n",
       "                    631327.11\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-17\",\n",
       "                    1090278.76\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-18\",\n",
       "                    1038466.15\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-19\",\n",
       "                    471186.66\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-20\",\n",
       "                    464138.13\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-21\",\n",
       "                    378256.57\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-22\",\n",
       "                    1280316.77\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-23\",\n",
       "                    414360.12\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-24\",\n",
       "                    419434.68\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-25\",\n",
       "                    764188.87\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-26\",\n",
       "                    622159.94\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-27\",\n",
       "                    547459.12\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-28\",\n",
       "                    1028526.38\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-29\",\n",
       "                    180081.55\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-30\",\n",
       "                    477645.84\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-31\",\n",
       "                    214127.27\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-01\",\n",
       "                    561533.79\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-02\",\n",
       "                    313786.92\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-03\",\n",
       "                    1058692.02\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-04\",\n",
       "                    275536.2\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-05\",\n",
       "                    258342.77\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-06\",\n",
       "                    343843.87\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-07\",\n",
       "                    1126023.76\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-08\",\n",
       "                    531558.37\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-09\",\n",
       "                    392192.99\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-10\",\n",
       "                    308197.78\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-11\",\n",
       "                    425164.5\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-12\",\n",
       "                    364595.14\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-13\",\n",
       "                    414495.94\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-14\",\n",
       "                    1258011.79\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-15\",\n",
       "                    896341.4\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-16\",\n",
       "                    447918.86\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-17\",\n",
       "                    277051.47\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-18\",\n",
       "                    1170974.33\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-19\",\n",
       "                    1143267.91\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-20\",\n",
       "                    275138.35\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-21\",\n",
       "                    118843.36\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"2020-01-05\",\n",
       "                \"2020-01-06\",\n",
       "                \"2020-01-07\",\n",
       "                \"2020-01-08\",\n",
       "                \"2020-01-09\",\n",
       "                \"2020-01-10\",\n",
       "                \"2020-01-11\",\n",
       "                \"2020-01-12\",\n",
       "                \"2020-01-13\",\n",
       "                \"2020-01-14\",\n",
       "                \"2020-01-15\",\n",
       "                \"2020-01-16\",\n",
       "                \"2020-01-17\",\n",
       "                \"2020-01-18\",\n",
       "                \"2020-01-19\",\n",
       "                \"2020-01-20\",\n",
       "                \"2020-01-21\",\n",
       "                \"2020-01-22\",\n",
       "                \"2020-01-23\",\n",
       "                \"2020-01-24\",\n",
       "                \"2020-01-25\",\n",
       "                \"2020-01-26\",\n",
       "                \"2020-01-27\",\n",
       "                \"2020-01-28\",\n",
       "                \"2020-01-29\",\n",
       "                \"2020-01-30\",\n",
       "                \"2020-01-31\",\n",
       "                \"2020-02-01\",\n",
       "                \"2020-02-02\",\n",
       "                \"2020-02-03\",\n",
       "                \"2020-02-04\",\n",
       "                \"2020-02-05\",\n",
       "                \"2020-02-06\",\n",
       "                \"2020-02-07\",\n",
       "                \"2020-02-08\",\n",
       "                \"2020-02-09\",\n",
       "                \"2020-02-10\",\n",
       "                \"2020-02-11\",\n",
       "                \"2020-02-12\",\n",
       "                \"2020-02-13\",\n",
       "                \"2020-02-14\",\n",
       "                \"2020-02-15\",\n",
       "                \"2020-02-16\",\n",
       "                \"2020-02-17\",\n",
       "                \"2020-02-18\",\n",
       "                \"2020-02-19\",\n",
       "                \"2020-02-20\",\n",
       "                \"2020-02-21\",\n",
       "                \"2020-02-22\",\n",
       "                \"2020-02-23\",\n",
       "                \"2020-02-24\",\n",
       "                \"2020-02-25\",\n",
       "                \"2020-02-26\",\n",
       "                \"2020-02-27\",\n",
       "                \"2020-02-28\",\n",
       "                \"2020-02-29\",\n",
       "                \"2020-03-01\",\n",
       "                \"2020-03-02\",\n",
       "                \"2020-03-03\",\n",
       "                \"2020-03-04\",\n",
       "                \"2020-03-05\",\n",
       "                \"2020-03-06\",\n",
       "                \"2020-03-07\",\n",
       "                \"2020-03-08\",\n",
       "                \"2020-03-09\",\n",
       "                \"2020-03-10\",\n",
       "                \"2020-03-11\",\n",
       "                \"2020-03-12\",\n",
       "                \"2020-03-13\",\n",
       "                \"2020-03-14\",\n",
       "                \"2020-03-15\",\n",
       "                \"2020-03-16\",\n",
       "                \"2020-03-17\",\n",
       "                \"2020-03-18\",\n",
       "                \"2020-03-19\",\n",
       "                \"2020-03-20\",\n",
       "                \"2020-03-21\",\n",
       "                \"2020-03-22\",\n",
       "                \"2020-03-23\",\n",
       "                \"2020-03-24\",\n",
       "                \"2020-03-25\",\n",
       "                \"2020-03-26\",\n",
       "                \"2020-03-27\",\n",
       "                \"2020-03-28\",\n",
       "                \"2020-03-29\",\n",
       "                \"2020-03-30\",\n",
       "                \"2020-03-31\",\n",
       "                \"2020-04-01\",\n",
       "                \"2020-04-02\",\n",
       "                \"2020-04-03\",\n",
       "                \"2020-04-04\",\n",
       "                \"2020-04-05\",\n",
       "                \"2020-04-06\",\n",
       "                \"2020-04-07\",\n",
       "                \"2020-04-08\",\n",
       "                \"2020-04-09\",\n",
       "                \"2020-04-10\",\n",
       "                \"2020-04-11\",\n",
       "                \"2020-04-12\",\n",
       "                \"2020-04-13\",\n",
       "                \"2020-04-14\",\n",
       "                \"2020-04-15\",\n",
       "                \"2020-04-16\",\n",
       "                \"2020-04-17\",\n",
       "                \"2020-04-18\",\n",
       "                \"2020-04-19\",\n",
       "                \"2020-04-20\",\n",
       "                \"2020-04-21\",\n",
       "                \"2020-04-22\",\n",
       "                \"2020-04-23\",\n",
       "                \"2020-04-24\",\n",
       "                \"2020-04-25\",\n",
       "                \"2020-04-26\",\n",
       "                \"2020-04-27\",\n",
       "                \"2020-04-28\",\n",
       "                \"2020-04-29\",\n",
       "                \"2020-04-30\",\n",
       "                \"2020-05-01\",\n",
       "                \"2020-05-02\",\n",
       "                \"2020-05-03\",\n",
       "                \"2020-05-04\",\n",
       "                \"2020-05-05\",\n",
       "                \"2020-05-06\",\n",
       "                \"2020-05-07\",\n",
       "                \"2020-05-08\",\n",
       "                \"2020-05-09\",\n",
       "                \"2020-05-10\",\n",
       "                \"2020-05-11\",\n",
       "                \"2020-05-12\",\n",
       "                \"2020-05-13\",\n",
       "                \"2020-05-14\",\n",
       "                \"2020-05-15\",\n",
       "                \"2020-05-16\",\n",
       "                \"2020-05-17\",\n",
       "                \"2020-05-18\",\n",
       "                \"2020-05-19\",\n",
       "                \"2020-05-20\",\n",
       "                \"2020-05-21\",\n",
       "                \"2020-05-22\",\n",
       "                \"2020-05-23\",\n",
       "                \"2020-05-24\",\n",
       "                \"2020-05-25\",\n",
       "                \"2020-05-26\",\n",
       "                \"2020-05-27\",\n",
       "                \"2020-05-28\",\n",
       "                \"2020-05-29\",\n",
       "                \"2020-05-30\",\n",
       "                \"2020-05-31\",\n",
       "                \"2020-06-01\",\n",
       "                \"2020-06-02\",\n",
       "                \"2020-06-03\",\n",
       "                \"2020-06-04\",\n",
       "                \"2020-06-05\",\n",
       "                \"2020-06-06\",\n",
       "                \"2020-06-07\",\n",
       "                \"2020-06-08\",\n",
       "                \"2020-06-09\",\n",
       "                \"2020-06-10\",\n",
       "                \"2020-06-11\",\n",
       "                \"2020-06-12\",\n",
       "                \"2020-06-13\",\n",
       "                \"2020-06-14\",\n",
       "                \"2020-06-15\",\n",
       "                \"2020-06-16\",\n",
       "                \"2020-06-17\",\n",
       "                \"2020-06-18\",\n",
       "                \"2020-06-19\",\n",
       "                \"2020-06-20\",\n",
       "                \"2020-06-21\",\n",
       "                \"2020-06-22\",\n",
       "                \"2020-06-23\",\n",
       "                \"2020-06-24\",\n",
       "                \"2020-06-25\",\n",
       "                \"2020-06-26\",\n",
       "                \"2020-06-27\",\n",
       "                \"2020-06-28\",\n",
       "                \"2020-06-29\",\n",
       "                \"2020-06-30\",\n",
       "                \"2020-07-01\",\n",
       "                \"2020-07-02\",\n",
       "                \"2020-07-03\",\n",
       "                \"2020-07-04\",\n",
       "                \"2020-07-05\",\n",
       "                \"2020-07-06\",\n",
       "                \"2020-07-07\",\n",
       "                \"2020-07-08\",\n",
       "                \"2020-07-09\",\n",
       "                \"2020-07-10\",\n",
       "                \"2020-07-11\",\n",
       "                \"2020-07-12\",\n",
       "                \"2020-07-13\",\n",
       "                \"2020-07-14\",\n",
       "                \"2020-07-15\",\n",
       "                \"2020-07-16\",\n",
       "                \"2020-07-17\",\n",
       "                \"2020-07-18\",\n",
       "                \"2020-07-19\",\n",
       "                \"2020-07-20\",\n",
       "                \"2020-07-21\",\n",
       "                \"2020-07-22\",\n",
       "                \"2020-07-23\",\n",
       "                \"2020-07-24\",\n",
       "                \"2020-07-25\",\n",
       "                \"2020-07-26\",\n",
       "                \"2020-07-27\",\n",
       "                \"2020-07-28\",\n",
       "                \"2020-07-29\",\n",
       "                \"2020-07-30\",\n",
       "                \"2020-07-31\",\n",
       "                \"2020-08-01\",\n",
       "                \"2020-08-02\",\n",
       "                \"2020-08-03\",\n",
       "                \"2020-08-04\",\n",
       "                \"2020-08-05\",\n",
       "                \"2020-08-06\",\n",
       "                \"2020-08-07\",\n",
       "                \"2020-08-08\",\n",
       "                \"2020-08-09\",\n",
       "                \"2020-08-10\",\n",
       "                \"2020-08-11\",\n",
       "                \"2020-08-12\",\n",
       "                \"2020-08-13\",\n",
       "                \"2020-08-14\",\n",
       "                \"2020-08-15\",\n",
       "                \"2020-08-16\",\n",
       "                \"2020-08-17\",\n",
       "                \"2020-08-18\",\n",
       "                \"2020-08-19\",\n",
       "                \"2020-08-20\",\n",
       "                \"2020-08-21\",\n",
       "                \"2020-08-22\",\n",
       "                \"2020-08-23\",\n",
       "                \"2020-08-24\",\n",
       "                \"2020-08-25\",\n",
       "                \"2020-08-26\",\n",
       "                \"2020-08-27\",\n",
       "                \"2020-08-28\",\n",
       "                \"2020-08-29\",\n",
       "                \"2020-08-30\",\n",
       "                \"2020-08-31\",\n",
       "                \"2020-09-01\",\n",
       "                \"2020-09-02\",\n",
       "                \"2020-09-03\",\n",
       "                \"2020-09-04\",\n",
       "                \"2020-09-05\",\n",
       "                \"2020-09-06\",\n",
       "                \"2020-09-07\",\n",
       "                \"2020-09-08\",\n",
       "                \"2020-09-09\",\n",
       "                \"2020-09-10\",\n",
       "                \"2020-09-11\",\n",
       "                \"2020-09-12\",\n",
       "                \"2020-09-13\",\n",
       "                \"2020-09-14\",\n",
       "                \"2020-09-15\",\n",
       "                \"2020-09-16\",\n",
       "                \"2020-09-17\",\n",
       "                \"2020-09-18\",\n",
       "                \"2020-09-19\",\n",
       "                \"2020-09-20\",\n",
       "                \"2020-09-21\",\n",
       "                \"2020-09-22\",\n",
       "                \"2020-09-23\",\n",
       "                \"2020-09-24\",\n",
       "                \"2020-09-25\",\n",
       "                \"2020-09-26\",\n",
       "                \"2020-09-27\",\n",
       "                \"2020-09-28\",\n",
       "                \"2020-09-29\",\n",
       "                \"2020-09-30\",\n",
       "                \"2020-10-01\",\n",
       "                \"2020-10-02\",\n",
       "                \"2020-10-03\",\n",
       "                \"2020-10-04\",\n",
       "                \"2020-10-05\",\n",
       "                \"2020-10-06\",\n",
       "                \"2020-10-07\",\n",
       "                \"2020-10-08\",\n",
       "                \"2020-10-09\",\n",
       "                \"2020-10-10\",\n",
       "                \"2020-10-11\",\n",
       "                \"2020-10-12\",\n",
       "                \"2020-10-13\",\n",
       "                \"2020-10-14\",\n",
       "                \"2020-10-15\",\n",
       "                \"2020-10-16\",\n",
       "                \"2020-10-17\",\n",
       "                \"2020-10-18\",\n",
       "                \"2020-10-19\",\n",
       "                \"2020-10-20\",\n",
       "                \"2020-10-21\",\n",
       "                \"2020-10-22\",\n",
       "                \"2020-10-23\",\n",
       "                \"2020-10-24\",\n",
       "                \"2020-10-25\",\n",
       "                \"2020-10-26\",\n",
       "                \"2020-10-27\",\n",
       "                \"2020-10-28\",\n",
       "                \"2020-10-29\",\n",
       "                \"2020-10-30\",\n",
       "                \"2020-10-31\",\n",
       "                \"2020-11-01\",\n",
       "                \"2020-11-02\",\n",
       "                \"2020-11-03\",\n",
       "                \"2020-11-04\",\n",
       "                \"2020-11-05\",\n",
       "                \"2020-11-06\",\n",
       "                \"2020-11-07\",\n",
       "                \"2020-11-08\",\n",
       "                \"2020-11-09\",\n",
       "                \"2020-11-10\",\n",
       "                \"2020-11-11\",\n",
       "                \"2020-11-12\",\n",
       "                \"2020-11-13\",\n",
       "                \"2020-11-14\",\n",
       "                \"2020-11-15\",\n",
       "                \"2020-11-16\",\n",
       "                \"2020-11-17\",\n",
       "                \"2020-11-18\",\n",
       "                \"2020-11-19\",\n",
       "                \"2020-11-20\",\n",
       "                \"2020-11-21\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u65e5\\u9500\\u552e\\u989d\\u66f2\\u7ebf\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_a4db56a340414b2280b125038c984795.setOption(option_a4db56a340414b2280b125038c984795);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x231bbe509a0>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    Line(init_opts=opts.InitOpts(theme=ThemeType.CHALK))\n",
    "    .add_xaxis(xaxis_data = df_sum_price_daily.index.strftime('%Y-%m-%d').tolist())\n",
    "    .add_yaxis(\"\", df_sum_price_daily['price'].tolist()) \n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"日销售额曲线\"))\n",
    "    .render_notebook()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 周销售额历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>event_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-05 00:00:00+00:00</th>\n",
       "      <td>1162961.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-12 00:00:00+00:00</th>\n",
       "      <td>8276254.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-19 00:00:00+00:00</th>\n",
       "      <td>6434036.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-26 00:00:00+00:00</th>\n",
       "      <td>7294314.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-02 00:00:00+00:00</th>\n",
       "      <td>6448754.34</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                price\n",
       "event_date                           \n",
       "2020-01-05 00:00:00+00:00  1162961.27\n",
       "2020-01-12 00:00:00+00:00  8276254.89\n",
       "2020-01-19 00:00:00+00:00  6434036.78\n",
       "2020-01-26 00:00:00+00:00  7294314.55\n",
       "2020-02-02 00:00:00+00:00  6448754.34"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sum_price_weekly = (df4.reindex(['event_date', 'price'], axis=1)\n",
    "                       .set_index('event_date')\n",
    "                       .groupby(pd.Grouper(freq='1W', level='event_date'))\n",
    "                       .sum()\n",
    "                      )\n",
    "df_sum_price_weekly.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min', 'chalk':'https://assets.pyecharts.org/assets/themes/chalk'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"d12693e02d7e4947ae34c01e7feaee83\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'chalk'], function(echarts) {\n",
       "                var chart_d12693e02d7e4947ae34c01e7feaee83 = echarts.init(\n",
       "                    document.getElementById('d12693e02d7e4947ae34c01e7feaee83'), 'chalk', {renderer: 'canvas'});\n",
       "                var option_d12693e02d7e4947ae34c01e7feaee83 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 10,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    \"2020-01-05\",\n",
       "                    1162961.27\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-12\",\n",
       "                    8276254.89\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-19\",\n",
       "                    6434036.78\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-01-26\",\n",
       "                    7294314.55\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-02\",\n",
       "                    6448754.34\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-09\",\n",
       "                    8095046.01\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-16\",\n",
       "                    8915668.74\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-02-23\",\n",
       "                    6502125.02\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-01\",\n",
       "                    6765352.5\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-08\",\n",
       "                    9612538.23\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-15\",\n",
       "                    10034185.9\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-22\",\n",
       "                    8217979.41\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-03-29\",\n",
       "                    7090458.2\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-05\",\n",
       "                    2629054.47\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-12\",\n",
       "                    1753943.61\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-19\",\n",
       "                    1603525.39\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-04-26\",\n",
       "                    1712130.99\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-03\",\n",
       "                    1945985.91\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-10\",\n",
       "                    3249173.79\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-17\",\n",
       "                    5555936.48\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-24\",\n",
       "                    8330571.97\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-05-31\",\n",
       "                    10288903.73\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-07\",\n",
       "                    11862049.15\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-14\",\n",
       "                    12066848.98\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-21\",\n",
       "                    9173119.5\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-06-28\",\n",
       "                    7868451.91\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-05\",\n",
       "                    10792496.01\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-12\",\n",
       "                    2922006.72\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-19\",\n",
       "                    4408198.53\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-07-26\",\n",
       "                    6255636.38\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-02\",\n",
       "                    8519371.49\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-09\",\n",
       "                    10466242.47\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-16\",\n",
       "                    8366085.01\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-23\",\n",
       "                    15192699.46\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-08-30\",\n",
       "                    14799248.26\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-06\",\n",
       "                    11279486.39\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-13\",\n",
       "                    12005176.17\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-20\",\n",
       "                    11708698.29\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-09-27\",\n",
       "                    9887403.87\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-04\",\n",
       "                    8654337.04\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-11\",\n",
       "                    4947228.74\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-18\",\n",
       "                    4754475.14\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-10-25\",\n",
       "                    4191881.8\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-01\",\n",
       "                    3631533.89\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-08\",\n",
       "                    3907783.91\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-15\",\n",
       "                    4058999.54\n",
       "                ],\n",
       "                [\n",
       "                    \"2020-11-22\",\n",
       "                    3433194.28\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"2020-01-05\",\n",
       "                \"2020-01-12\",\n",
       "                \"2020-01-19\",\n",
       "                \"2020-01-26\",\n",
       "                \"2020-02-02\",\n",
       "                \"2020-02-09\",\n",
       "                \"2020-02-16\",\n",
       "                \"2020-02-23\",\n",
       "                \"2020-03-01\",\n",
       "                \"2020-03-08\",\n",
       "                \"2020-03-15\",\n",
       "                \"2020-03-22\",\n",
       "                \"2020-03-29\",\n",
       "                \"2020-04-05\",\n",
       "                \"2020-04-12\",\n",
       "                \"2020-04-19\",\n",
       "                \"2020-04-26\",\n",
       "                \"2020-05-03\",\n",
       "                \"2020-05-10\",\n",
       "                \"2020-05-17\",\n",
       "                \"2020-05-24\",\n",
       "                \"2020-05-31\",\n",
       "                \"2020-06-07\",\n",
       "                \"2020-06-14\",\n",
       "                \"2020-06-21\",\n",
       "                \"2020-06-28\",\n",
       "                \"2020-07-05\",\n",
       "                \"2020-07-12\",\n",
       "                \"2020-07-19\",\n",
       "                \"2020-07-26\",\n",
       "                \"2020-08-02\",\n",
       "                \"2020-08-09\",\n",
       "                \"2020-08-16\",\n",
       "                \"2020-08-23\",\n",
       "                \"2020-08-30\",\n",
       "                \"2020-09-06\",\n",
       "                \"2020-09-13\",\n",
       "                \"2020-09-20\",\n",
       "                \"2020-09-27\",\n",
       "                \"2020-10-04\",\n",
       "                \"2020-10-11\",\n",
       "                \"2020-10-18\",\n",
       "                \"2020-10-25\",\n",
       "                \"2020-11-01\",\n",
       "                \"2020-11-08\",\n",
       "                \"2020-11-15\",\n",
       "                \"2020-11-22\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5468\\u9500\\u552e\\u989d\\u66f2\\u7ebf\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_d12693e02d7e4947ae34c01e7feaee83.setOption(option_d12693e02d7e4947ae34c01e7feaee83);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x231bd788d60>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    Line(init_opts=opts.InitOpts(theme=ThemeType.CHALK))\n",
    "    .add_xaxis(xaxis_data = df_sum_price_weekly.index.strftime('%Y-%m-%d').tolist())\n",
    "    .add_yaxis(\"\", df_sum_price_weekly['price'].tolist(),\n",
    "               is_symbol_show=True, # 显示点\n",
    "               symbol_size=10 # 设置点尺寸\n",
    "              ) \n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"周销售额曲线\"))\n",
    "    .render_notebook()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 月销售额历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_month</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01</td>\n",
       "      <td>26494398.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-02</td>\n",
       "      <td>31836933.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-03</td>\n",
       "      <td>37550969.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-04</td>\n",
       "      <td>7689624.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-05</td>\n",
       "      <td>28346976.05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  event_month        price\n",
       "0     2020-01  26494398.53\n",
       "1     2020-02  31836933.60\n",
       "2     2020-03  37550969.39\n",
       "3     2020-04   7689624.61\n",
       "4     2020-05  28346976.05"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sum_price_monthly = (df4.reindex(['event_date', 'price'], axis=1)\n",
    "                        .set_index('event_date')\n",
    "                        .groupby(pd.Grouper(freq='1M', level='event_date'))\n",
    "                        .sum()\n",
    "                        .reset_index()\n",
    "                        .eval(\"event_month = event_date.dt.strftime('%Y-%m')\", engine='python')\n",
    "                        .reindex(['event_month', 'price'], axis=1)\n",
    "                      )\n",
    "df_sum_price_monthly.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min', 'chalk':'https://assets.pyecharts.org/assets/themes/chalk'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"35de0f81b4d643b4b09dd81d72a90e0f\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'chalk'], function(echarts) {\n",
       "                var chart_35de0f81b4d643b4b09dd81d72a90e0f = echarts.init(\n",
       "                    document.getElementById('35de0f81b4d643b4b09dd81d72a90e0f'), 'chalk', {renderer: 'canvas'});\n",
       "                var option_35de0f81b4d643b4b09dd81d72a90e0f = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                26494398.53,\n",
       "                31836933.6,\n",
       "                37550969.39,\n",
       "                7689624.61,\n",
       "                28346976.05,\n",
       "                45073779.59,\n",
       "                26207763.15,\n",
       "                52825929.1,\n",
       "                49317988.81,\n",
       "                19765680.76,\n",
       "                11961511.52\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": false,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"2020-01\",\n",
       "                \"2020-02\",\n",
       "                \"2020-03\",\n",
       "                \"2020-04\",\n",
       "                \"2020-05\",\n",
       "                \"2020-06\",\n",
       "                \"2020-07\",\n",
       "                \"2020-08\",\n",
       "                \"2020-09\",\n",
       "                \"2020-10\",\n",
       "                \"2020-11\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u6708\\u9500\\u552e\\u989d\\u5386\\u53f2\\u6570\\u636e\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_35de0f81b4d643b4b09dd81d72a90e0f.setOption(option_35de0f81b4d643b4b09dd81d72a90e0f);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x231c2d2a5e0>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.CHALK))\n",
    "    .add_xaxis(df_sum_price_monthly['event_month'].to_list())\n",
    "    .add_yaxis(\"\", df_sum_price_monthly['price'].to_list())\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"月销售额历史数据\"))\n",
    "    .render_notebook()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;从日销售额, 周销售额，月销售额图来看，2020年4月是一个波谷，或许与当地新冠疫情态势有关，可能当地在4月疫情形势严峻。如果要排除这种可能性，需要结合当地卫生部的疫情数据，当地防疫措施。才能确定是否是疫情导致的2020年4月销售额暴跌。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 季度销售额历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_quarter</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-Q1</td>\n",
       "      <td>9.588230e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-Q2</td>\n",
       "      <td>8.111038e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-Q3</td>\n",
       "      <td>1.283517e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-Q4</td>\n",
       "      <td>3.172719e+07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  event_quarter         price\n",
       "0       2020-Q1  9.588230e+07\n",
       "1       2020-Q2  8.111038e+07\n",
       "2       2020-Q3  1.283517e+08\n",
       "3       2020-Q4  3.172719e+07"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sum_price_quarterly = (df4.reindex(['event_date', 'price'], axis=1)\n",
    "                          .set_index('event_date')\n",
    "                          .groupby(pd.Grouper(freq='1Q', level='event_date'))\n",
    "                          .sum()\n",
    "                          .reset_index()\n",
    "                          .eval(\"event_year = event_date.dt.year.astype('string')\", engine='python')\n",
    "                          .eval(\"event_quarter = event_date.dt.quarter.astype('string')\", engine='python')\n",
    "                          .eval(\"event_quarter = event_year.str.cat(event_quarter, sep='-Q')\", engine='python')\n",
    "                          .reindex(['event_quarter', 'price'], axis=1)\n",
    "                         )\n",
    "df_sum_price_quarterly.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min', 'chalk':'https://assets.pyecharts.org/assets/themes/chalk'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"5513787fa8c747fe8dbaf06cc7e0b4d5\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'chalk'], function(echarts) {\n",
       "                var chart_5513787fa8c747fe8dbaf06cc7e0b4d5 = echarts.init(\n",
       "                    document.getElementById('5513787fa8c747fe8dbaf06cc7e0b4d5'), 'chalk', {renderer: 'canvas'});\n",
       "                var option_5513787fa8c747fe8dbaf06cc7e0b4d5 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"2020-Q1\",\n",
       "                    \"value\": 95882301.52\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"2020-Q2\",\n",
       "                    \"value\": 81110380.25\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"2020-Q3\",\n",
       "                    \"value\": 128351681.06\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"2020-Q4\",\n",
       "                    \"value\": 31727192.28\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"40%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {c}\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"2020-Q1\",\n",
       "                \"2020-Q2\",\n",
       "                \"2020-Q3\",\n",
       "                \"2020-Q4\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"type\": \"scroll\",\n",
       "            \"show\": true,\n",
       "            \"right\": \"20%\",\n",
       "            \"bottom\": \"20%\",\n",
       "            \"orient\": \"vertical\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5b63\\u5ea6\\u9500\\u552e\\u989d\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_5513787fa8c747fe8dbaf06cc7e0b4d5.setOption(option_5513787fa8c747fe8dbaf06cc7e0b4d5);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x231c0660940>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    Pie(init_opts=opts.InitOpts(theme=ThemeType.CHALK))\n",
    "    .add(\n",
    "        \"\",\n",
    "        df_sum_price_quarterly.values.tolist(),\n",
    "        center=[\"40%\", \"50%\"]\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"季度销售额\"),\n",
    "        legend_opts=opts.LegendOpts(type_=\"scroll\", pos_right=\"20%\", pos_bottom=\"20%\", orient=\"vertical\") # 设置垂直图例\n",
    "    )\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(formatter=\"{b}: {c}\"))\n",
    "    .render_notebook()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 时间序列回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由于商品种类较多，高达2万多种，单独对每一种进行预测不现实，只能对关注度和销售额占比较高的商品进行预测。但是目前只有1年的数据，且由于新冠疫情这种突发事件影响，进行时间序列回归建模方差会很大，没有多大意义。所以这里就不建模了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 关联规则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;关联规则属于机器学习的一种，也被称为'购物篮分析', 用于找出商品与商品之间销量的关系，便于制订针对性的营销策略，如捆绑销售，将存在正相关关联的2种商品，紧挨着摆放着货架上。如果是电商，则图片放在一起。关联规则有几个重要概念：<br/>\n",
    "\n",
    "* **支持度**, Support, 表示项集(可以看作1笔订单内的所有商品种类)在事务(1个事务就相当于1笔订单)中出现的概率，用于评价项集在给定数据集中的出现频繁程度。\n",
    "* **置信度**, Confidence, 2个项集A和B，其中$A\\sqcap B = \\varnothing $，计算公式为$s(A, B)/s(A)$, 置信度表示$A \\rightarrow B$出现的频繁程度。用于评价规则的可靠性。\n",
    "* **频繁项集**, Frequent itemset, 设定支持度和置信度阈值后，就能确定满足阈值的所有项集，这就是频繁项集，再根据频繁项集产生强规则(strong rules)。\n",
    "* 在寻找频繁项集的过程中，因为数据量很大，所以需要剪枝，以去掉没必要项集。**Apriori算法**就是基于支持度进行剪枝(pruning)。能够控制项集数量的指数级增长。基于支持度的剪枝最重要的2条定理就是：<br/>\n",
    "    * 如果1个项集是频繁的，则其所有子集也是频繁的。\n",
    "    * 如果1个项集是非频繁的，则其超集也是非频繁的。这就是支持度度量的**反单调性**, 据此可以剪掉大量不必要的枝。\n",
    "* **FP增长算法**, FP树搜索频繁项集的方法与Apriori算法完全不同，其是通过逐个读取事务，然后映射到FP树中的一条路径来构造。FP树中每个节点都包括1个项的标记和计数，计数映射到事务。然后根据路径的重叠效果来搜索频繁项集，通常支持度越高，项集越频繁。可以看出，FP增长算法数据量和计算量并不会随事务的增加而指数级增长。因此运行速度比Apriori算法快几个数量级。\n",
    "* 支持度和置信度只是代表规则的出现的概率和可靠性。**并不代表因果关系**。对于海量的电商数据，通常满足支持度和置信度阈值的规则也是海量的。还需要**兴趣度量**和**相关性度量**2个方面的评价指标，才能进一步筛选规则，最后作出相对准确可靠的判断。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split"
   },
   "source": [
    "<table class=\"tg\">\n",
    "<thead>\n",
    "  <tr>\n",
    "    <th class=\"tg-0pky\"></th>\n",
    "    <th class=\"tg-0pky\">B</th>\n",
    "    <th class=\"tg-0pky\">\\begin{equation}\\bar{B}\\end{equation}</th>\n",
    "    <th class=\"tg-0pky\"></th>\n",
    "    <th class=\"tg-0pky\"></th>\n",
    "  </tr>\n",
    "</thead>\n",
    "<tbody>\n",
    "  <tr>\n",
    "    <td class=\"tg-0pky\">A</td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{11}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{10}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{1+}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\"></td>\n",
    "  </tr>\n",
    "  <tr>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}\\bar{A}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{01}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{00}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{0+}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\"></td>\n",
    "  </tr>\n",
    "  <tr>\n",
    "    <td class=\"tg-0pky\"></td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{+1}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\">\\begin{equation}f_{+0}\\end{equation}</td>\n",
    "    <td class=\"tg-0pky\">N</td>\n",
    "    <td class=\"tg-0pky\"></td>\n",
    "  </tr>\n",
    "</tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split"
   },
   "source": [
    "二路联表中:<br/>\n",
    "* 其中$f_{ij}$表示频率计数，\n",
    "* $f_{11}$表示A和B同时出现在一个事务中的次数, \n",
    "* $f_{01}$表示包含A但不包含B的事务的个数。\n",
    "* $f_{1+}$表示行之和，即A的支持度计数,\n",
    "* $f_{+1}$表示列之和，即B的支持度计数。\n",
    "* 联合概率$P(A,B)=s(A,B)=\\frac {f_{11}}{N}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **提升度**，lift, 也叫做'兴趣因子'。公式如下：<br/>\n",
    "$$I(A, B) = \\frac {s(A,B)}{s(A) \\times s(B)} = \\frac {Nf_{11}}{f_{1+}f_{+1}}$$\n",
    "从上式可以看出，若$I(A, B)=1$, 则A和B是独立的，若$I(A, B)>1$, 则A和B是正相关的，若$I(A, B)<1$, 则A和B是负相关的。\n",
    "提升度比置信度具有更多的统计学优势。\n",
    "\n",
    "* **杠杆率**, leverage, 也叫做'Piatetsky-Shapiro(PS)度量'。与提升度比较，不是计算比值，而是计算差值来评价相关性。公式如下:<br/>\n",
    "$$PS = s(A, B) - s(A) \\times s(B) = \\frac {f_{11}}{N} - \\frac {f_{1+}f_{+1}}{N^2}$$\n",
    "从上式可以看出，若$PS=0$,则A与B相互独立，若$PS>0$, 则正相关，若$PS<0$, 则负相关。\n",
    "\n",
    "* **相关性**, 对于连续变量，使用Pearson相关系数，即$corr(x,y)=\\frac {s_{xy}}{s_x s_y}$, 其中$s_{xy}为协方差$, $s_x$和$s_y$为方差。对于离散二元变量，使用$\\phi$系数进行度量。公式如下:<br/>\n",
    "$$\\phi = \\frac {f_{11}f_{00} - f_{01}f_{10}}{\\sqrt {f_{1+}f_{+1}f_{0+}f_{+0}}} = \\frac {Nf_{11} - f_{1+}f_{+1}}{\\sqrt {f_{1+}f_{+1}f_{0+}f_{+0}}} $$ \n",
    "$$\\phi = \\frac {s(A, B) - s(A)\\times s(B)}{\\sqrt {s(A) \\times (1-s(A)) \\times s(B) \\times (1-s(B))}}$$ \n",
    "从上式可以看出，$\\phi$系数可以理解为PS度量的归一化版本，$\\phi$系数的值范围为$-1 \\sim +1$。若$\\phi=0$, 则表示相互独立，$\\phi=1$则表示完全正相关，$\\phi=-1$则表示完全负相关。\n",
    "\n",
    "* **全置信度**, conviction, 公式如下: <br/>\n",
    "$$h\\text{-}confidence = min\\left [ \\frac {f_{11}}{f_{1+}}, \\frac {f_{11}}{f_{+1}} \\right ]$$\n",
    "$$h\\text{-}confidence = \\frac {1-s(B)}{1-s(A, B)/s(A)} = \\frac {1-s(B)}{1-confidence(A \\rightarrow B)}$$\n",
    "范围为$\\left [0, +\\infty \\right]$\n",
    "\n",
    "* **交叉支持模式**, cross-support, 对于倾斜分布的数据集，如3个哑变量，p, q, r, p的支持度为83.8%, q和r的支持度都为16.7%, 但是q和r在p中不是均匀分布的，而是倾斜分布的，少量的p中集中了所有的q和r。所以尽管q和r的支持度低，但是q和r却表现为强烈的相关性。对于这种数据集。设置支持度阈值非常棘手。如果阈值太高，则会遗漏q和r这种频繁项集，如果阈值太低，首先计算量和数据量非暴增，其次，提取的规则数量会大幅度增加，尤其是会增加很多虚假的规则, 使得分析和解释非常困难，如高频的p与低频的q相关联的规则。这是因为p与q之间的关联性大部分受p项的频繁发生而不是p与q共同出现的影响。由于{p, q}的支持度与{q, r}的支持度非常接近，当阈值设定较低时，更容易得到规则{p, q}。\n",
    "\n",
    "* **使用全置信度指标，可以删除交叉支持模式**，理论是，如果某个规则存在交叉支持模式，那么将前件(antecedents)和后件(consequents)交换位置，绝不会增加规则的置信度。即移除置信度增加的规则即可。\n",
    "\n",
    "* **辛普森悖论**, 通过对隐藏变量分层后分别建模，可以发生一对变量之间的关联消失或方向逆转。原因是分层前的关联模式虚假的，增加分层就能避免。\n",
    "\n",
    "* **费舍尔精确检验**，Fisher Exact Test, 假设有2项集分别为A和B，零假设为A和B相互独立，\n",
    "* **卡方$\\chi ^2$检验**, Chi-Squared Test, 可以包含2个以上项的项集，可以使用卡方检验，零假设为项集X中所有项都具有统计独立性。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算事务集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先计算所有事务集，通过订单号分组，然后去重，转换为列表即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2186014, 2)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(2185340, 2)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>order_id</th>\n",
       "      <th>product_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>1515966223509089906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2294584263154074236</td>\n",
       "      <td>2273948316817424439</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2295716521449619559</td>\n",
       "      <td>1515966223509261697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2295740594749702229</td>\n",
       "      <td>1515966223509104892</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              order_id           product_id\n",
       "0  2294359932054536986  1515966223509089906\n",
       "2  2294444024058086220  2273948319057183658\n",
       "4  2294584263154074236  2273948316817424439\n",
       "5  2295716521449619559  1515966223509261697\n",
       "6  2295740594749702229  1515966223509104892"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5 = df4.reindex(['order_id', 'product_id'], axis=1)\n",
    "df5.shape\n",
    "df5.duplicated().any() # 是否存在重复项目\n",
    "df5 = df5.drop_duplicates()\n",
    "df5.shape\n",
    "df5.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1407334, 2)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>order_id</th>\n",
       "      <th>product_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2294359932054536986</td>\n",
       "      <td>[1515966223509089906]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2294444024058086220</td>\n",
       "      <td>[2273948319057183658]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2294584263154074236</td>\n",
       "      <td>[2273948316817424439]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2295716521449619559</td>\n",
       "      <td>[1515966223509261697]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2295740594749702229</td>\n",
       "      <td>[1515966223509104892]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              order_id             product_id\n",
       "0  2294359932054536986  [1515966223509089906]\n",
       "1  2294444024058086220  [2273948319057183658]\n",
       "2  2294584263154074236  [2273948316817424439]\n",
       "3  2295716521449619559  [1515966223509261697]\n",
       "4  2295740594749702229  [1515966223509104892]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df6 = (df5.set_index('order_id')\n",
    "       .groupby(level='order_id')['product_id']\n",
    "       .agg(lambda x: x.sort_values().to_list())\n",
    "       .reset_index()\n",
    "       .sort_index()\n",
    "      )\n",
    "df6.shape\n",
    "df6.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['1515966223509089906'],\n",
       " ['2273948319057183658'],\n",
       " ['2273948316817424439'],\n",
       " ['1515966223509261697'],\n",
       " ['1515966223509104892'],\n",
       " ['2273948311742316796']]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df6_list = df6['product_id'].to_list()\n",
    "df6_list[:6]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 哑变量矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先使用`TransactionEncoder`将数据转换为哑变量矩阵, 数据太大，商品种类太多, 使用稀疏矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n# 非稀疏矩阵\\nte_ary = te.fit(df6_list).transform(df6_list)\\ndf = pd.DataFrame(te_ary, columns=te.columns_)\\n'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(1407334, 23782)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "te = TransactionEncoder()\n",
    "oht_ary = te.fit(df6_list).transform(df6_list, sparse=True)\n",
    "sparse_df = pd.DataFrame.sparse.from_spmatrix(oht_ary, columns=te.columns_)\n",
    "\n",
    "\"\"\"\n",
    "# 非稀疏矩阵\n",
    "te_ary = te.fit(df6_list).transform(df6_list)\n",
    "df = pd.DataFrame(te_ary, columns=te.columns_)\n",
    "\"\"\"\n",
    "sparse_df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "哑变量矩阵达到了140多万行，2万多个列。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FP-增长算法计算频繁项集\n",
    "因为数据集太大，使用FP增长算法计算频繁项集更合适。因为数据量很大，所以设置支持度阈值很小。支持度阈值过大，结果就是空集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 4.23 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "frequent_items = fpgrowth(sparse_df, min_support=0.002, use_colnames=True, max_len=3) # 项集元素最多3个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(63, 2)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>0.009943</td>\n",
       "      <td>(1515966223509117074)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0.008476</td>\n",
       "      <td>(1515966223509106786)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>0.006625</td>\n",
       "      <td>(2273948186349404174)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.006300</td>\n",
       "      <td>(1515966223509088521)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0.006287</td>\n",
       "      <td>(2273948316473492113)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     support               itemsets\n",
       "37  0.009943  (1515966223509117074)\n",
       "32  0.008476  (1515966223509106786)\n",
       "49  0.006625  (2273948186349404174)\n",
       "26  0.006300  (1515966223509088521)\n",
       "21  0.006287  (2273948316473492113)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frequent_items.sort_values(by='support', ascending=False, inplace=True)\n",
    "frequent_items.shape\n",
    "frequent_items.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最大支持度才不到1/1000, 考虑到订单数量2万多，算下来最大频次计数约为20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Apriori算法版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing 9 combinations | Sampling itemset size 2\n",
      "Wall time: 1.6 s\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0.009943</td>\n",
       "      <td>(1515966223509117074)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.008476</td>\n",
       "      <td>(1515966223509106786)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>0.006625</td>\n",
       "      <td>(2273948186349404174)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.006300</td>\n",
       "      <td>(1515966223509088521)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>0.006287</td>\n",
       "      <td>(2273948316473492113)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     support               itemsets\n",
       "32  0.009943  (1515966223509117074)\n",
       "29  0.008476  (1515966223509106786)\n",
       "45  0.006625  (2273948186349404174)\n",
       "0   0.006300  (1515966223509088521)\n",
       "57  0.006287  (2273948316473492113)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "frequent_items2 = apriori(sparse_df, min_support=0.002, use_colnames=True, max_len=3, verbose=1, low_memory=True)\n",
    "frequent_items2.shape\n",
    "frequent_items2.sort_values(by='support', ascending=False).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结果可以看到Apriori算法与FP增长算法得到的频繁项集相同。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算规则和支持度\n",
    "支持多个指标剪枝，包括support, confidence, lift, leverage, conviction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>phi</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(2273948222722409184)</td>\n",
       "      <td>(2273948297037087396)</td>\n",
       "      <td>0.004094</td>\n",
       "      <td>0.004989</td>\n",
       "      <td>0.002915</td>\n",
       "      <td>0.712203</td>\n",
       "      <td>142.758458</td>\n",
       "      <td>0.002895</td>\n",
       "      <td>3.457334</td>\n",
       "      <td>0.643542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(2273948297037087396)</td>\n",
       "      <td>(2273948222722409184)</td>\n",
       "      <td>0.004989</td>\n",
       "      <td>0.004094</td>\n",
       "      <td>0.002915</td>\n",
       "      <td>0.584390</td>\n",
       "      <td>142.758458</td>\n",
       "      <td>0.002895</td>\n",
       "      <td>2.396251</td>\n",
       "      <td>0.643542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(1515966223527275752)</td>\n",
       "      <td>(1515966223523303307)</td>\n",
       "      <td>0.002711</td>\n",
       "      <td>0.005977</td>\n",
       "      <td>0.002199</td>\n",
       "      <td>0.811271</td>\n",
       "      <td>135.726305</td>\n",
       "      <td>0.002183</td>\n",
       "      <td>5.266940</td>\n",
       "      <td>0.544682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(1515966223523303307)</td>\n",
       "      <td>(1515966223527275752)</td>\n",
       "      <td>0.005977</td>\n",
       "      <td>0.002711</td>\n",
       "      <td>0.002199</td>\n",
       "      <td>0.367927</td>\n",
       "      <td>135.726305</td>\n",
       "      <td>0.002183</td>\n",
       "      <td>1.577806</td>\n",
       "      <td>0.544682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(2273948218662322996)</td>\n",
       "      <td>(2273948218662322995)</td>\n",
       "      <td>0.004302</td>\n",
       "      <td>0.006104</td>\n",
       "      <td>0.002713</td>\n",
       "      <td>0.630657</td>\n",
       "      <td>103.311096</td>\n",
       "      <td>0.002687</td>\n",
       "      <td>2.690986</td>\n",
       "      <td>0.527029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(2273948218662322995)</td>\n",
       "      <td>(2273948218662322996)</td>\n",
       "      <td>0.006104</td>\n",
       "      <td>0.004302</td>\n",
       "      <td>0.002713</td>\n",
       "      <td>0.444419</td>\n",
       "      <td>103.311096</td>\n",
       "      <td>0.002687</td>\n",
       "      <td>1.792173</td>\n",
       "      <td>0.527029</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             antecedents            consequents  antecedent support  \\\n",
       "1  (2273948222722409184)  (2273948297037087396)            0.004094   \n",
       "0  (2273948297037087396)  (2273948222722409184)            0.004989   \n",
       "5  (1515966223527275752)  (1515966223523303307)            0.002711   \n",
       "4  (1515966223523303307)  (1515966223527275752)            0.005977   \n",
       "3  (2273948218662322996)  (2273948218662322995)            0.004302   \n",
       "2  (2273948218662322995)  (2273948218662322996)            0.006104   \n",
       "\n",
       "   consequent support   support  confidence        lift  leverage  conviction  \\\n",
       "1            0.004989  0.002915    0.712203  142.758458  0.002895    3.457334   \n",
       "0            0.004094  0.002915    0.584390  142.758458  0.002895    2.396251   \n",
       "5            0.005977  0.002199    0.811271  135.726305  0.002183    5.266940   \n",
       "4            0.002711  0.002199    0.367927  135.726305  0.002183    1.577806   \n",
       "3            0.006104  0.002713    0.630657  103.311096  0.002687    2.690986   \n",
       "2            0.004302  0.002713    0.444419  103.311096  0.002687    1.792173   \n",
       "\n",
       "        phi  \n",
       "1  0.643542  \n",
       "0  0.643542  \n",
       "5  0.544682  \n",
       "4  0.544682  \n",
       "3  0.527029  \n",
       "2  0.527029  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rules = association_rules(frequent_items, metric='confidence', min_threshold=0.1)\n",
    "# 计算相关性\n",
    "phi_numerator = rules['support'] - rules['antecedent support']*rules['consequent support']\n",
    "phi_denominator = np.sqrt(rules['antecedent support'] * \\\n",
    "                          (1-rules['antecedent support']) * \\\n",
    "                          rules['consequent support'] * \\\n",
    "                          (1-rules['consequent support']))\n",
    "rules['phi'] = phi_numerator/ phi_denominator\n",
    "# 按相关性和置信度降序排列\n",
    "rules.sort_values(by=['phi', 'conviction', 'confidence'], ascending=False, inplace=True)\n",
    "rules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上表可以看出：\n",
    "* 第1条规则，$2273948222722409184 \\rightarrow 2273948297037087396$比较合理, 首先前件和后件的支持度都较大，订单数量达到了80以上。置信度达到了71%， 相关性phi达到了0.64。          \n",
    "* 第2条规则，$2273948297037087396 \\rightarrow 2273948222722409184$就是第1条规则倒过来，交换前件和后件的位置。只是置信度稍微低了一点。\n",
    "* 第3条规则和第4条规则也只是前件和后件顺序不一样。虽然第3条置信度较高，但是前件支持度远低于后件支持度，这表示这条规则是虚假模式。\n",
    "\n",
    "总之，商品2273948222722409184与2273948297037087396存在很强的关联性。值得进一步分析。其它关联性并不强。  \n",
    "我们将对应的商品分类和商标提取出来看看。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>product_id</th>\n",
       "      <th>category_id</th>\n",
       "      <th>category_code</th>\n",
       "      <th>brand</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>28835</th>\n",
       "      <td>2273948222722409184</td>\n",
       "      <td>2268105542972998580</td>\n",
       "      <td>furniture.bedroom.pillow</td>\n",
       "      <td>dogland</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29033</th>\n",
       "      <td>2273948297037087396</td>\n",
       "      <td>2268105538568979268</td>\n",
       "      <td>NaN</td>\n",
       "      <td>goodride</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                product_id          category_id             category_code  \\\n",
       "28835  2273948222722409184  2268105542972998580  furniture.bedroom.pillow   \n",
       "29033  2273948297037087396  2268105538568979268                       NaN   \n",
       "\n",
       "          brand  \n",
       "28835   dogland  \n",
       "29033  goodride  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(df1.query(\"product_id in ['2273948222722409184', '2273948297037087396']\")\n",
    " .reindex(['product_id', 'category_id', 'category_code', 'brand'], axis=1)\n",
    " .drop_duplicates()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果有商品名称等更多信息，就能与销售部门一起分析制订营销策略。但是商品编号2273948297037087396没有这些信息。所以分析只能到这一步了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"color:blue; font-size:200%; font-weight:bold\">参考来源:</p>\n",
    "\n",
    "* [mlxtend github](http://rasbt.github.io/mlxtend/)\n",
    "* [mlxtend apriori](http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/apriori/)\n",
    "* [mlxtend fpgrowth](http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/fpgrowth/)\n",
    "* [mlxtend association_rules](http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/association_rules/)\n",
    "* [Python数据科学：卡方检验](https://cloud.tencent.com/developer/article/1709992)\n",
    "* [Python统计分析-卡方检验](https://blog.csdn.net/qq_38214903/article/details/82967812)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py37_for_ml_and_visual",
   "language": "python",
   "name": "py37_for_ml_and_visual"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "409.6px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
